home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl2 / examples / sgi_extensions / sharpen_texture.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  5.2 KB  |  211 lines

  1. /*
  2.  * Copyright 1996, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18. /* sharpen_texutre.c - simple program to demonstrate texture sharpening
  19.  *
  20.  *    Escape key        - exit the program
  21.  *    <s> key            - cycle through sharpen filters
  22.  */
  23.  
  24. #include <GL/gl.h>
  25. #include <GL/glu.h>
  26. #include <GL/glut.h>
  27.  
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30.  
  31. /*  Function Prototypes  */
  32.  
  33. GLvoid  initgfx( GLvoid );
  34. GLvoid  drawScene( GLvoid );
  35. GLvoid  reshape( GLsizei, GLsizei );
  36. GLvoid  keyboard( GLubyte, GLint, GLint );
  37.  
  38. void printHelp( char * );
  39.  
  40. /* Global Definitions */
  41.  
  42. #define KEY_ESC    27    /* ascii value for the escape key */
  43.  
  44. /* tree texture: high alpha in foreground, zero alpha in background */
  45. #define B 0x00000000
  46. #define F 0xA0A0A0ff
  47.  
  48. /* Global Variables */
  49.  
  50. static unsigned int tex[] = {
  51.     B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,
  52.     B,B,B,B,B,B,B,F,F,B,B,B,B,B,B,B,
  53.     B,B,B,B,B,B,B,F,F,B,B,B,B,B,B,B,
  54.     B,B,B,B,B,B,F,F,F,F,B,B,B,B,B,B,
  55.     B,B,B,B,B,B,F,F,F,F,B,B,B,B,B,B,
  56.     B,B,B,B,B,F,F,F,F,F,F,B,B,B,B,B,
  57.     B,B,B,B,B,F,F,F,F,F,F,B,B,B,B,B,
  58.     B,B,B,B,F,F,F,F,F,F,F,F,B,B,B,B,
  59.     B,B,B,B,F,F,F,F,F,F,F,F,B,B,B,B,
  60.     B,B,B,F,F,F,F,F,F,F,F,F,F,B,B,B,
  61.     B,B,B,F,F,F,F,F,F,F,F,F,F,B,B,B,
  62.     B,B,F,F,F,F,F,F,F,F,F,F,F,F,B,B,
  63.     B,B,F,F,F,F,F,F,F,F,F,F,F,F,B,B,
  64.     B,B,B,B,B,B,F,F,F,F,B,B,B,B,B,B,
  65.     B,B,B,B,B,B,F,F,F,F,B,B,B,B,B,B,
  66.     B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,
  67. };
  68.  
  69. static GLboolean sharpen_texture_supported = GL_TRUE;
  70.  
  71. GLvoid
  72. main( int argc, char *argv[] )
  73. {
  74.     GLsizei            width, height;
  75.  
  76.     glutInit( &argc, argv );
  77.  
  78.     width = glutGet(GLUT_SCREEN_WIDTH); 
  79.     height = glutGet(GLUT_SCREEN_HEIGHT);
  80.     glutInitWindowPosition( width / 4, height / 4); 
  81.     glutInitWindowSize( width / 2, height / 2 );
  82.     glutInitDisplayMode( GLUT_RGBA );
  83.     glutCreateWindow( argv[0] );
  84.  
  85.     initgfx();
  86.  
  87.     glutKeyboardFunc( keyboard );
  88.     glutReshapeFunc( reshape );
  89.     glutDisplayFunc( drawScene );
  90.  
  91.     printHelp( argv[0] );
  92.  
  93.     glutMainLoop();
  94. }
  95.  
  96. GLvoid
  97. printHelp( char *progname )
  98. {
  99.     fprintf(stdout, "\n%s - uses sharpen texture extension to display "
  100.         "a texture\n\n"
  101.         "<s> key            - cycle through sharpen filters\n"
  102.         "Escape key        - exit the program\n\n",
  103.         progname);
  104.  
  105.     fprintf( stdout, "Magnification filter is GL_LINEAR\n" );
  106. }
  107.  
  108. void
  109. initgfx(void)
  110. {
  111.     /* use alpha test to discard background pixels */
  112.     glEnable(GL_ALPHA_TEST);
  113.     glAlphaFunc(GL_GEQUAL, 0.3);
  114.  
  115.     glEnable(GL_TEXTURE_2D);
  116.  
  117.     glClearColor(0.0, 0.0, 0.0, 1.0);
  118.  
  119.     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  120.     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  121.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  122.  
  123.     if (!glutExtensionSupported("GL_SGIS_sharpen_texture")) {
  124.         fprintf(stderr,
  125.            "\nGL_SGIS_sharpen_texture not supported on this machine\n");
  126.         sharpen_texture_supported = GL_FALSE;
  127.     }
  128.  
  129.     /* generate mipmaps; levels 0 and 1 are needed for sharpening */
  130.     gluBuild2DMipmaps(GL_TEXTURE_2D, 4, 16, 16, GL_RGBA, GL_UNSIGNED_BYTE,
  131.         tex);
  132. }
  133.  
  134. void
  135. cycleMagFilter( void )
  136. {
  137.     static GLint filter = GL_LINEAR;
  138.  
  139. #ifdef GL_SGIS_sharpen_texture
  140.     if (sharpen_texture_supported) {
  141.         switch (filter) {
  142.         case GL_LINEAR:
  143.             filter = GL_LINEAR_SHARPEN_SGIS;
  144.             fprintf( stdout, 
  145.                 "Magnification filter is GL_LINEAR_SHARPEN_SGIS\n" );
  146.             break;
  147.         case GL_LINEAR_SHARPEN_SGIS:
  148.             /* sharpening just alpha is useful for keeping the tree 
  149.              * outline crisp 
  150.              */
  151.             filter = GL_LINEAR_SHARPEN_ALPHA_SGIS;
  152.             fprintf( stdout, 
  153.                 "Magnification filter is GL_LINEAR_SHARPEN_ALPHA_SGIS\n" );
  154.             break;
  155.         case GL_LINEAR_SHARPEN_ALPHA_SGIS:
  156.             filter = GL_LINEAR_SHARPEN_COLOR_SGIS;
  157.             fprintf( stdout, 
  158.                 "Magnification filter is GL_LINEAR_SHARPEN_COLOR_SGIS\n" );
  159.             break;
  160.         case GL_LINEAR_SHARPEN_COLOR_SGIS:
  161.             filter = GL_LINEAR;
  162.             fprintf( stdout, 
  163.                 "Magnification filter is GL_LINEAR\n" );
  164.             break;
  165.         }
  166.     } else {
  167.         printf("GL_SGIS_sharpen_texture is not supported\n");
  168.     }
  169. #endif
  170.  
  171.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter );
  172. }
  173.  
  174. GLvoid 
  175. keyboard( GLubyte key, GLint x, GLint y )
  176. {
  177.     switch (key) {
  178.     case 's':
  179.         cycleMagFilter();
  180.         glutPostRedisplay();
  181.         break;
  182.     case KEY_ESC:    /* Exit whenever the Escape key is pressed */
  183.         exit(0);
  184.     }
  185. }
  186.  
  187. GLvoid
  188. reshape( GLsizei width, GLsizei height )
  189. {
  190.     glViewport(0, 0, width, height);
  191.     glMatrixMode(GL_PROJECTION);
  192.     gluPerspective(60.0, (GLdouble)width/height, 1.0, 10.0 );
  193.     glMatrixMode(GL_MODELVIEW);
  194.     glTranslatef(0.,0.,-2.5);
  195. }
  196.  
  197. void
  198. drawScene( GLvoid )
  199. {
  200.     glClear(GL_COLOR_BUFFER_BIT);
  201.     glBegin(GL_TRIANGLE_STRIP);
  202.         glTexCoord2f( 0, 1); glVertex2f(-1,-1);
  203.         glTexCoord2f( 0, 0); glVertex2f(-1, 1);
  204.         glTexCoord2f( 1, 1); glVertex2f( 1,-1);
  205.         glTexCoord2f( 1, 0); glVertex2f( 1, 1);
  206.     glEnd();
  207.     glFlush();
  208.  
  209.     checkError("drawScene");
  210. }
  211.